/* --- 全局变量 --- */

let timeout = 300,
  topDistance = 30;

/* --- --- */

/* --- 自定义弹出层动画 --- */

function loadLayoutSize() {
  let layoutBox = $('.layout-box');
  $.each(layoutBox, function (i) {
    layoutBox.eq(i).css('margin-top', '-' + (layoutBox.eq(i).height() / 2 + topDistance) + 'px');
  });
}

loadLayoutSize();

function showLayout(elem) {
  let layoutBox = elem.children('.layout-box');
  elem.children('.layout-background').removeClass('display-none').animate({
    'opacity': .5
  }, timeout);
  layoutBox.removeClass('display-none').animate({
    'margin-top': '-' + (layoutBox.height() / 2) + 'px',
    'opacity': 1
  }, timeout);
}

function hideLayout(elem) {
  let layoutBox = elem.children('.layout-box');
  elem.children('.layout-background').animate({
    'opacity': 0
  }, timeout);
  layoutBox.animate({
    'margin-top': '-' + (layoutBox.height() / 2 + topDistance) + 'px',
    'opacity': 0
  }, timeout);
  setTimeout(() => {
    elem.children('.layout-background, .layout-box').addClass('display-none');
    elem.find('input, textarea').val('');
  }, timeout);
}

/* --- --- */

/* --- 页面跳转相关函数 --- */

let urlArray = {
  provider: () => window.location.href = '/provider',
  type: () => window.location.href = '/type',
  info: () => window.location.href = '/goodInfo',
  warehouse: () => window.location.href = '/warehouse',
  order: () => window.location.href = '/order',
  logout: () => window.location.href = '/logout'
};

/* --- --- */

/* --- 验证用户是否登录 --- */

function isLogin() {
  $.get('/isLogin', emp => {
    if (emp === '') {
      urlArray.logout();
    } else {
      $('.layui-header .layui-nav-item a:eq(0)').text(emp.name);
    }
  });
}

isLogin();

/* --- --- */

/* --- 时间格式化函数 --- */

function formatTime(time) {
  return new Date(time);
}

/* --- --- */

/* --- 自定义正则验证函数 --- */

function regexPhone(phone) {
  return /1[2-9][0-9]{9}/ig.test(phone) && phone.length === 11;
}

function regexDecimal(number) {
  return /^\d{1,9}(\.{0}|\.\d{1,2})?$/ig.test(number);
}

/* --- --- */